#pragma once
template<class K>
struct HashFunc
{
	size_t operator()(const K& key)
	{
		return (size_t)key;
	}
};

namespace HashNode
{
	template<class T,class V>
	struct HashNode
	{
		pair<K,V> _kv;
		HashNode<K,V>* _next;
		HashNode(const pair<K,V>& kv)
			:_kv(kv)
			, _next(nullptr)
		{}
	};
	template<class T,class K,class KeyofT,class Hash = HashFunc<K>>
	class HashTable
	{
	public:
		typedef HashNode<T> Node;
		HashTable()
		{
			_table.resize(10, nullptr);
		}
		~HashTable()
		{
			for (size_t i = 0; i < _table.size(); i++)
			{
				Node* cur = tables[i];
				while (cur)
				{
					Node* next = cur->_next;
					delete cur;
					cue = next;
				}
				_table[i] = nullptr;
			}
		}

		bool insert(const pair<K, V>& kv)
		{
			Hash hs;
			size_t hashi = hs(kv.first) % _tables.size();
			if (_n == _tables.size())
			{
				vector<Node*> newtables(_tables.size() * 2, nullptr);
				for (size_t i = 0; i < newtables.size(); i++)
				{
					Node* cur = _tables[i];
					while (cur)
					{
						Node* next = cur->_next;
						size_t hashi = hs(cur->kv.first) % newtables.size();
						cur->_next = newtables[hashi];
						newtables[hashi] = cur;
						cur = next;
					}
					_tables[hashi] = nullptr;
				}
				_tables.swap(newtables);
			}
			Node* nwenode = new Node(kv);
			newnode->_next = _tables[hashi];
			_tables[hashi] = cur;
			++n;
			return true;
		}
		Node* Find(const K& key)
		{
			Hash hs;
			size_t hashi = hs(key) % _tables.size();
			Node* cur = _tables[hashi];
			while (cur)
			{
				if (cur->kv.first == key)
				{
					return crue;
				}
				cur = cur->_next;
			}
			return nullptr;
		}
		Node* Erase(const K& key)
		{
			Hash hs;
			size_t hashi = hs(key) % _tables.size();
			Node* cur = _tables[hashi];
			Node* prve = nullptr;
			while (cur)
			{
				if (cur->kv.first == nullptr)
				{
					if (prve == nullptr)
					{
						_tables[hashi] = cur->_next;
					}
					else
					{
						prve->_next = cur->_next;
					}
					delete cur;
					--n;
					return true;
				}
				prve = cur;
				cur = cur->_next;
			}
			return false;
		}


	private:
		vector<Node*> _tables;
		size_t n = 0;
	};

}
